package com.bacon.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.bacon.gmall.list.client.ListFeignClient;
import com.bacon.gmall.model.product.BaseCategoryView;
import com.bacon.gmall.model.product.SkuInfo;
import com.bacon.gmall.model.product.SpuSaleAttr;
import com.bacon.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2020-12-23 19:38
 * @description: 商品详情页面汇总
 */
@RestController
@RequestMapping("/api/item")
public class ItemApiController {


    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    //获取商品详情页面
    @GetMapping("/getItem/{skuId}")
    public Map getItem(@PathVariable Long skuId) {

        HashMap<String, Object> map = new HashMap<>();

        //获取skuInfo
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {

            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        //价格
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {

            BigDecimal price = productFeignClient.getPrice(skuId);
            map.put("price", price);
        }, threadPoolExecutor);

        //组合 对应skuId
        CompletableFuture<Void> valuesSkuJson = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {

            Map spu = productFeignClient.getSaleAttrValuesBySpu(skuInfo.getSpuId());
            map.put("valuesSkuJson", JSONObject.toJSONString(spu));
        });

        //三级分类
        CompletableFuture<Void> categoryView = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {

            BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", baseCategoryView);
        });

        //销售属性及属性值
        CompletableFuture<Void> spuSaleAttrList = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {

            List<SpuSaleAttr> spuSaleAttrChecked = productFeignClient.getSpuSaleAttrChecked(skuInfo.getSpuId(), skuInfo.getId());
            map.put("spuSaleAttrList", spuSaleAttrChecked);
        });

        //更新热度，每点击一次热度+1
        CompletableFuture.runAsync(()->{
            listFeignClient.hotScore(skuId);
        },threadPoolExecutor);

        CompletableFuture.allOf(
                spuSaleAttrList,
                categoryView,
                valuesSkuJson,
                voidCompletableFuture,
                skuInfoCompletableFuture
        ).join();

        return map;
    }


}
